package runesim.testing;
import java.net.*;
import java.io.*;
public class WeightRipperZyb {

			public static void main(String[] args){
				try {
					BufferedWriter out = new BufferedWriter(new FileWriter(new File("output.txt")));
					
					
					loadDefinitions();
					System.out.println("loaded definitions");
					
					for(int i = 0; i < 5162; i++){
						harvestPage(new URL("http://www.zybez.net/items.php?id="+i), out);
						System.out.println(i+" fin");
					}
					
					//harvestPage(new URL("http://www.zybez.net/items.php?id="+4738), out);
					
					out.close();
					System.out.println("Done!");
				} catch(Exception e){
					System.out.println("Exception occured:");
					e.printStackTrace();
				}
			}
			
			public static void loadDefinitions(){
				try {
					BufferedReader in = new BufferedReader(new FileReader("ITEMS.list"));
					int index = 0;
					String line;
					
					while((line = in.readLine()) != null){ //Read line
						if(!line.startsWith("item")) continue;
						
						int id = Integer.parseInt(line.substring(line.indexOf("item = ")+7, line.indexOf("\t")));
						String name = line.substring(line.indexOf("\t")+1, line.indexOf("\t", line.indexOf("\t")+1));
						name = name.replaceAll("_", " ");
					
						defs[index++] = new ItemDef(name, id);
					}
					
					System.out.println("defs loaded: "+index);
					
					in.close();
				} catch(Exception e){
					System.out.println("Failed loading definitions: "+e);
					e.printStackTrace();
					System.exit(1);
				}
			}
			
			public static void harvestPage(URL url, BufferedWriter out){
				try {
					BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
					String[] pageCache = new String[500];
					int cacheIndex = 0;
					String linex = null;
					
					while((linex = in.readLine()) != null){ //parse page
						pageCache[cacheIndex++] = linex;
					}
					in.close();
					
					System.out.println("page loaded");
					
					String itemName = null;
					double itemWeight = -65535D;
					
					for(int i = 0; i < cacheIndex; i++){
						if(pageCache[i] != null){
							String line = pageCache[i];
							if(line.startsWith("&raquo; <a href=\"/items.php\">Item Database BETA</a> &raquo; <u>")){
								//Found our name initializer line
								
								itemName = line.substring(line.indexOf("&raquo; <a href=\"/items.php\">Item Database BETA</a> &raquo; <u>")+63, line.indexOf("</u>"));
								System.out.println("name of item: "+itemName);
							} else if(line.startsWith("<center><a href=\"/correction.php?area=items")){
								//Found our weight initializer line
								String weightString = line.substring(line.indexOf("<td>Weight:</td><td>")+20, line.indexOf("kg</td>", line.indexOf("<td>Weight:</td><td>")));
								itemWeight = Double.parseDouble(weightString);
								System.out.println("weight of item: "+itemWeight);
							}
						}
					}
					
					
					if(itemName != null & itemWeight != -65535D){ //Correctly read
						if(doesNameExist(itemName)){ //Woot!
							int id = getIDForName(itemName);
							out.write(id+"\t\t"+itemWeight);
							out.newLine();
							out.flush();
						} else {
							//too new or wrong format maybe?
						}
					}
				} catch(Exception e){
					System.out.println("Parse error: "+e); //quiet, don't print exceptions this time
					e.printStackTrace(); //remove
				}
			}
			
			public static String stripNameLine(String nameLine){
				return nameLine.substring(nameLine.indexOf("\t\t\t")+3, nameLine.indexOf("\t\t\t</td>"));
			}
			
			public static boolean doesNameExist(String n){
				for(int i = 0; i < defs.length; i++){
					if(defs[i] != null){
						if(defs[i].getName().equalsIgnoreCase(n)){
							return true;
						} else continue;
					}
				}
				
				return false;
			}
			
			public static int getIDForName(String n){
				for(int i = 0; i < defs.length; i++){
					if(defs[i] != null){
						if(defs[i].getName().equalsIgnoreCase(n)){
							return defs[i].getID();
						} else continue;
					}
				}
				
				return -1; //Won't EVER happen hopefully
			}
		
			public static String stripWeightLine(String wLine){
				return wLine.substring(wLine.indexOf("\t\t\t\t")+4, wLine.indexOf("\tkg\t	  </td>"));
			}
			
			public static ItemDef[] defs = new ItemDef[7933];
			
			public static class ItemDef {
				private String name;
				private int id;
				
				public ItemDef(String n, int i){
					this.name = n;
					this.id = i;
				}
				
				public String getName(){
					return name;
				}
				
				public int getID(){
					return id;
				}
			}
}
